/* 
	小程序支付调用
 */
import request from '../js/ajax.js'

const pay={
	func(url='',data={}){
		request({
			url:url,
			data:data,
			method:'post',
			success(res){
				
				if(res.statusCode==200){
					
					if(res.data.code==1){
						
						if(res.data.data.type=='notPay'){
							uni.showToast({
								title: "支付成功",
								duration: 3000,
								icon: 'none',
							})
						}else{
							let data = res.data
							console.log(data)
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: data.data.data.info.timeStamp,
								nonceStr: data.data.data.info.nonceStr,
								package: data.data.data.info.package,
								signType: data.data.data.info.signType,
								paySign: data.data.data.info.paySign,
								success: function(res) {
									uni.showToast({
										title: "支付成功",
										duration: 3000,
										icon: 'none',
									});
								},
								fail: function(err) {
									/* uni.showModal({
										content: "支付失败,原因为:\n " + err.errMsg,
										showCancel: false,
									}) */
									uni.showModal({
										content: "支付失败",
										showCancel: false,
									})
									// console.log('fail:' + JSON.stringify(err));
								}
							})
						}
						
					}else{
						uni.showModal({
							content: res.data.msg,
							showCancel: false,
						})
					}
					
				}else{
					uni.showModal({
						content: "网络繁忙",
						showCancel: false,
					})
				}

			},
			fail(){
				uni.showModal({
					content: "请求失败",
					showCancel: false,
				})
			}
		})
	}
}

export default pay